package com.neusoft.elmboot.service.impl;

import java.math.BigDecimal;
import com.neusoft.elmboot.mapper.WalletMapper;
import com.neusoft.elmboot.po.VirtualWallet;
import com.neusoft.elmboot.po.VirtualWalletDomain;
import com.neusoft.elmboot.service.VirtualWalletService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neusoft.elmboot.po.VirtualWalletTransaction;

@Service
public class VirtualWalletImpl implements VirtualWalletService{
	@Autowired
	 private WalletMapper walletMapper; 
	public VirtualWallet getWallet(String userId) {
		// TODO Auto-generated method stub
		return walletMapper.getWallet(userId);
	}

	@Transactional
	@Override
	public void debit(String userId, BigDecimal amount)  {
		// TODO Auto-generated method stub
		VirtualWalletDomain wallet = new VirtualWalletDomain(walletMapper.getWallet(userId));
		try {
			wallet.debit(amount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		walletMapper.updateBalance(userId, wallet.getbalance());
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
		Date date = new Date(System.currentTimeMillis());
		VirtualWalletTransaction transaction = new VirtualWalletTransaction();
		transaction.setTransactionTime(formatter.format(date));
		transaction.setTransactionId(UUID.randomUUID().toString().replace("-", ""));
		transaction.setTransactionType(1);
        transaction.setOutId(userId);
        transaction.setInId(userId);
        if(amount.compareTo(wallet.getbalance()) <= 0) {
        	walletMapper.saveTransaction(transaction.getTransactionId(), transaction.getTransactionTime(), amount, transaction.getTransactionType(), transaction.getInId(), transaction.getOutId());
        }       
	}
	@Transactional
	@Override
	public void credit(String userId, BigDecimal amount) {
		// TODO Auto-generated method stub
		VirtualWalletDomain wallet = new VirtualWalletDomain(walletMapper.getWallet(userId));

		try {
			wallet.credit(amount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		walletMapper.updateBalance(userId,wallet.getbalance());
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
		Date date = new Date(System.currentTimeMillis());
		VirtualWalletTransaction transaction = new VirtualWalletTransaction();
		transaction.setTransactionTime(formatter.format(date));
		transaction.setTransactionId(UUID.randomUUID().toString().replace("-", ""));
		transaction.setTransactionType(0);
        transaction.setInId(userId);
        transaction.setOutId(userId);
        if(amount.compareTo(BigDecimal.ZERO) >= 0) {
        walletMapper.saveTransaction(transaction.getTransactionId(), transaction.getTransactionTime(), amount, transaction.getTransactionType(), transaction.getInId(), transaction.getOutId());
        }
        }
	
	@Transactional
	@Override
	public void transaction(String inId,String outId,BigDecimal amount){
		VirtualWalletDomain inwallet = new VirtualWalletDomain(walletMapper.getWallet(inId));
		VirtualWalletDomain outwallet = new VirtualWalletDomain(walletMapper.getWallet(outId));
		try {
			outwallet.debit(amount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		walletMapper.updateBalance(outId, outwallet.getbalance());
		try {
			inwallet.credit(amount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		walletMapper.updateBalance(inId,inwallet.getbalance());
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
		Date date = new Date(System.currentTimeMillis());
		VirtualWalletTransaction transaction = new VirtualWalletTransaction();
		transaction.setTransactionTime(formatter.format(date));
		transaction.setTransactionId(UUID.randomUUID().toString().replace("-", ""));
		transaction.setTransactionType(2);
        transaction.setOutId(outId);
        transaction.setInId(inId);
        if(amount.compareTo(BigDecimal.ZERO) >= 0) {
       walletMapper.saveTransaction(transaction.getTransactionId(), transaction.getTransactionTime(), amount, transaction.getTransactionType(), transaction.getInId(), transaction.getOutId());
      }
     }
	
	@Override
	public void updateBalance(String walletId, BigDecimal amount) {
		// TODO Auto-generated method stub
		 walletMapper.updateBalance(walletId, amount);
	}
	
	@Override
	public void createWallet(String userId,BigDecimal balance) {
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
		Date date = new Date(System.currentTimeMillis());
		walletMapper.createWallet(userId, formatter.format(date), balance);
	}
	
}
